package org.scc.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.scc.model.MeetingPlace;
import org.scc.service.MeetingPlaceService;

public class MeetingPlaceAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private MeetingPlaceService meetingPlaceService;
	
	private MeetingPlace meetingPlace;
	
	public MeetingPlaceAction(){
		meetingPlaceService = new MeetingPlaceService();
	}
	
	public void listMeetingPlaces(){
		List<MeetingPlace> meetingPlaces = meetingPlaceService.findAll();
		responeString(getMeetingPlaceJson(meetingPlaces));
	}
	
	private String getMeetingPlaceJson(List<MeetingPlace> meetingPlaces){
		JSONArray arr = new JSONArray();
		JSONObject obj = null;
		for(MeetingPlace mp : meetingPlaces){
			obj = new JSONObject();
			obj.put("id", mp.getId());
			obj.put("name", mp.getPlace());
			obj.put("remark", mp.getRemark() );
			arr.add(obj);
		}
		return arr.toString();
	}
	
	public String initEditPage(){
		if(id != 0){
			meetingPlace = meetingPlaceService.load(id);
		}
		return SUCCESS;
	}
	
	public void editMeetingPlace(){
		if(meetingPlace.getId() == 0){
			meetingPlaceService.add(meetingPlace);
		} else {
			meetingPlaceService.update(meetingPlace);
		}
		responseSuccess();
	}

	public void deleteMeetingPlace(){
		meetingPlaceService.delete(ids);
		responseSuccess();
	}

	public MeetingPlace getMeetingPlace() {
		return meetingPlace;
	}

	public void setMeetingPlace(MeetingPlace meetingPlace) {
		this.meetingPlace = meetingPlace;
	}
	
}
